﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SAPbouiCOM;
using System.Collections;
using SAPbobsCOM;
using BoAddonBiz_FreightForwarder.BusinessBase;

namespace BoAddonBiz_FreightForwarder
{
    /// <summary>
    /// 费用方案窗体
    /// </summary>
    [ControlB1FormAttribute(IsNew = true, FormType = B1FormTypes.ado_AVA_FF_FEEScenario)]
    class B1Form_FeeSolution : SAPBoAddon.B1Form
    {
        private SAPbouiCOM.BoFormMode CurrentMode;
        #region constructor
        public B1Form_FeeSolution()
        {
            this.frm_ItemEvent += new SAPBoAddon.IB1FormEvents.B1ItemEventEventHandler(B1Form_FeeScenario_frm_ItemEvent);
            this.frm_ChooseFormListEvent += new SAPBoAddon.IB1FormEvents.B1ChooseFormListEventEventHandler(B1Form_FeeScenario_frm_ChooseFormListEvent);
            this.frm_MenuEvent += new SAPBoAddon.IB1FormEvents.B1MenuEventEventHandler(B1Form_FeeScenario_frm_MenuEvent);
        }
        #endregion
        #region overload
        protected override bool BeforeSaveCheckFormData()
        {
            if (string.IsNullOrEmpty(SAPBoAddon.B1Assistant.Item.GetValue(CurrentForm, "6").ToString().Trim()))
            {
                SBOApp.MessageBox("请给此费用方案一个名称，方便日后的使用。");
                return false;
            }
            return true;
        }
        protected override void FormEditModeChange(SAPBoAddon.frmItemEditMode frmEditMode)
        {
            switch (frmEditMode)
            {
                case SAPBoAddon.frmItemEditMode.fe_AddMode:
                    SAPbouiCOM.DBDataSource dbds = CurrentForm.DataSources.DBDataSources.Item("@AVA_FF_OFEE");
                    dbds.SetValue("U_DocDate", dbds.Offset, DateTime.Now.ToString("yyyyMMdd"));
                    SAPBoAddon.B1Assistant.Matrix.AutoAddNewLine(this.CurrentForm, "mtx_1", "col_f", "@AVA_FF_FEE1", -1, "Lineid", "Lineid", true);
                    break;
            }
        }
        protected override void FormInfoSetting(SAPBoAddon.B1FormSetting FormSetting)
        {
            FormSetting.FormType = B1FormTypes.ado_AVA_FF_FEEScenario;
            FormSetting.FormFileName = B1FormTypes.ado_AVA_FF_FEEScenario.ToString() + "Form.srf";

            FormSetting.AutoGetObjectNextNumber = true;
            #region MatrixSetting
            FormSetting.MatrixSetting.MtxUID = "mtx_1";
            FormSetting.MatrixSetting.NotAllowNoLineSave = false;
            FormSetting.MatrixSetting.AutoAddNewLine = false;
            FormSetting.MatrixSetting.MenuAddNewLine = false;
            FormSetting.MatrixSetting.AutoLineKeyCol = "col_2";
            FormSetting.MatrixSetting.BeforeSaveFixDbDataSourceData = true;
            FormSetting.MatrixSetting.BeforeSaveDeleteNoValueLine = true;
            FormSetting.MatrixSetting.TableKeyField = "LineId";
            FormSetting.MatrixSetting.TableVisOrder = "VisOrder";
            FormSetting.MatrixSetting.PrimaryLineCol = "col_f";
            FormSetting.MatrixSetting.TableName = "@AVA_FF_FEE1";
            FormSetting.MatrixSetting.Add();

            FormSetting.MatrixSetting.MtxUID = "mtx_2";
            FormSetting.MatrixSetting.AutoAddNewLine = false;
            FormSetting.MatrixSetting.MenuAddNewLine = false;
            FormSetting.MatrixSetting.AutoLineKeyCol = "col_15";
            FormSetting.MatrixSetting.BeforeSaveFixDbDataSourceData = true;
            FormSetting.MatrixSetting.BeforeSaveDeleteNoValueLine = true;
            FormSetting.MatrixSetting.TableKeyField = "LineId";
            FormSetting.MatrixSetting.TableVisOrder = "VisOrder";
            FormSetting.MatrixSetting.PrimaryLineCol = "col_f";
            FormSetting.MatrixSetting.TableName = "@AVA_FF_FEE2";
            FormSetting.MatrixSetting.Add();
            #endregion
        }
        protected override void InitializeForm(SAPBoAddon.B1FormInitializePar InitPar)
        {
            Utils.InitialMatrixComboBox(SBOCompany, this.CurrentForm.Items.Item("mtx_1").Specific, "col_12", "Code", "Name", "@AVA_FF_CATY");//物料类型
            Utils.InitialMatrixComboBox(this.SBOCompany, this.CurrentForm.Items.Item("mtx_2").Specific, "col_17", "DocCurrCod", "CurrName", "OCRN");//订单金额币种
        }
        #endregion
        #region 事件
        void B1Form_FeeScenario_frm_ChooseFormListEvent(SAPBoAddon.B1AddonBase.B1ChooseFormList pVal, ref bool BubbleEvent)
        {
            SAPbouiCOM.Matrix omtx = default(SAPbouiCOM.Matrix);
            SAPbouiCOM.DBDataSource dbds = default(SAPbouiCOM.DBDataSource);
            SAPbobsCOM.Recordset oRst = (SAPbobsCOM.Recordset)SBOCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset);

            if (pVal.BeforeAction == false)
            {
                switch (pVal.ChooseFromListUID)
                {
                    //费用
                    case "CFL_ItemCode":
                        dbds = CurrentForm.DataSources.DBDataSources.Item("@AVA_FF_FEE1");
                        omtx = (SAPbouiCOM.Matrix)this.CurrentForm.Items.Item("mtx_1").Specific;
                        if (pVal.SelectedObjects.Rows.Count > 1)
                        {
                            int CurrentRow = pVal.Row;
                            omtx.GetLineData(CurrentRow);
                            string CargoType = dbds.GetValue("U_CargoType", SAPBoAddon.B1Assistant.Matrix.GetdbDsOffset(omtx, CurrentRow, "col_f", dbds)).Trim();
                            for (int i = 0; i < pVal.SelectedObjects.Rows.Count; i++)
                            {
                                dbds.Offset = SAPBoAddon.B1Assistant.Matrix.GetdbDsOffset(omtx, CurrentRow, "col_f", dbds);
                                dbds.SetValue("U_CargoType", dbds.Offset, CargoType);
                                dbds.SetValue("U_ItemCode", dbds.Offset, pVal.SelectedObjects.GetValue("ItemCode", i).ToString());
                                dbds.SetValue("U_ItemName", dbds.Offset, pVal.SelectedObjects.GetValue("ItemName", i).ToString());
                                omtx.SetLineData(CurrentRow);
                                SAPBoAddon.B1Assistant.Matrix.AutoAddNewLine(this.CurrentForm, "mtx_1", "col_f", "@AVA_FF_FEE1", CurrentRow, "Lineid", "Lineid", true);
                                CurrentRow += 1;
                            }
                        }
                        else
                        {
                            dbds.Offset = SAPBoAddon.B1Assistant.Matrix.GetdbDsOffset(omtx, pVal.Row, "col_f", dbds);
                            omtx.GetLineData(pVal.Row);
                            string FeeCode = pVal.SelectedObjects.GetValue("ItemCode", 0).ToString();
                            dbds.SetValue("U_ItemCode", dbds.Offset, FeeCode);
                            dbds.SetValue("U_ItemName", dbds.Offset, pVal.SelectedObjects.GetValue("ItemName", 0).ToString());
                            omtx.SetLineData(pVal.Row);
                            //复制费用条件模板
                            string LineId = dbds.GetValue("LineId", dbds.Offset).Trim();
                            Utils.CopyConditionTemplate(CurrentForm, "mtx_2", "@AVA_FF_FEE2", LineId, FeeCode);
                            SAPBoAddon.B1Assistant.Matrix.AutoAddNewLine(this.CurrentForm, "mtx_1", "col_f", "@AVA_FF_FEE1", -1, "Lineid", "Lineid", true, "col_2");
                        }
                        break;
                    //航空公司
                    case "CFL_AirLine_Fee":
                        dbds = this.CurrentForm.DataSources.DBDataSources.Item("@AVA_FF_FEE1");
                        SAPbouiCOM.Matrix mtx = (SAPbouiCOM.Matrix)this.CurrentForm.Items.Item(pVal.ItemUID).Specific;
                        dbds.Offset = SAPBoAddon.B1Assistant.Matrix.GetdbDsOffset(mtx, pVal.Row, "col_f", dbds);
                        mtx.GetLineData(pVal.Row);
                        dbds.SetValue("U_CarrierCode", dbds.Offset, pVal.SelectedObjects.GetValue("U_AirLineCode", 0).ToString());
                        dbds.SetValue("U_CarrierName", dbds.Offset, pVal.SelectedObjects.GetValue("U_AirLineName", 0).ToString());
                        mtx.SetLineData(pVal.Row);
                        break;
                }
            }
        }
        void B1Form_FeeScenario_frm_ItemEvent(SAPBoAddon.B1AddonBase.B1ItemEvent pVal, ref bool BubbleEvent)
        {
            if (pVal.BeforeAction)
            {
                if (pVal.ItemUID == "1" && pVal.EventType == BoEventTypes.et_ITEM_PRESSED)
                {
                    if (CurrentForm.Mode == BoFormMode.fm_ADD_MODE || CurrentForm.Mode == BoFormMode.fm_UPDATE_MODE)
                    {
                        SAPbouiCOM.Matrix oMtx2 = CurrentForm.Items.Item("mtx_2").Specific;
                        oMtx2.LoadFromDataSourceEx();
                    }
                }
                if (pVal.ItemUID == "mtx_1" && pVal.EventType == BoEventTypes.et_CLICK)
                {
                    CurrentMode = CurrentForm.Mode;
                }
            }
            else
            {
                #region 汇率设置
                if (pVal.ItemUID == "mtx_2" && pVal.ColUID == "col_17" && pVal.EventType == BoEventTypes.et_COMBO_SELECT)//只在添加状态下？
                {
                    Utils.SetRateByCurrency(this.CurrentForm, this.SBOCompany, pVal.ItemUID, "col_17", "col_18", pVal.Row, this.CurrentForm.DataSources.DBDataSources.Item("@AVA_FF_OFEE").GetValue("U_DocDate", 0));
                }
                #endregion
                #region 选中获得焦点的行
                if (pVal.ItemUID == "mtx_1" && pVal.EventType == BoEventTypes.et_CLICK)
                {
                    this.SelectedRow(pVal.ColUID, pVal.Row);
                    if (CurrentMode == BoFormMode.fm_OK_MODE || CurrentMode == BoFormMode.fm_VIEW_MODE)
                        CurrentForm.Mode = CurrentMode;
                }
                #endregion
                #region 条件值自动添加行
                if (pVal.ItemUID == "mtx_2" && pVal.ColUID == "col_15" && pVal.EventType == BoEventTypes.et_VALIDATE)
                {
                    AddRow();
                }
                #endregion
            }
        }

        void B1Form_FeeScenario_frm_MenuEvent(SAPBoAddon.B1AddonBase.B1MenuEvent pVal, ref bool BubbleEvent)
        {
            if (pVal.BeforeAction)
            {
                if (pVal.ItemUID == "mtx_1" && pVal.MenuUID == "1292")//费用项添加行
                {
                    SAPBoAddon.B1Assistant.Matrix.AutoAddNewLine(this.CurrentForm, "mtx_1", "col_f", "@AVA_FF_FEE1", -1, "Lineid", "Lineid", true);
                }
                if (pVal.ItemUID == "mtx_1" && pVal.MenuUID == "1293")//费用项删除行
                {
                    DeleteRow();
                    BubbleEvent = false;
                }
                if (pVal.ItemUID == "mtx_2" && pVal.MenuUID == "1292")//添加行
                {
                    AddRow();
                }
                if (pVal.ItemUID == "mtx_2" && pVal.MenuUID == "1293")//删除行
                {
                    SAPBoAddon.B1Assistant.Matrix.DeleteRowAndDbdsLine(CurrentForm, "@AVA_FF_FEE2", "Lineid", "mtx_2", "col_f", pVal.Row, true);
                    BubbleEvent = false;
                }
            }
        }
        #endregion
        #region private methods
        /// <summary>
        /// 执行条件添加行
        /// </summary>
        private void AddRow()
        {
            if (CurrentForm.Mode == BoFormMode.fm_FIND_MODE) return;
            SAPbouiCOM.Matrix oMtx1 = CurrentForm.Items.Item("mtx_1").Specific;
            SAPbouiCOM.Matrix oMtx2 = CurrentForm.Items.Item("mtx_2").Specific;
            //SAPbouiCOM.DBDataSource dbds = CurrentForm.DataSources.DBDataSources.Item("@AVA_FF_FEE2");
            int SelectedRow = oMtx1.GetNextSelectedRow();
            if (SelectedRow <= 0)
            {
                SBOApp.MessageBox("请选择要为哪一个费用项添加执行条件。");
                return;
            }
            string Lineid = string.Empty;
            dynamic item = oMtx1.GetCellSpecific("col_f", SelectedRow);
            Lineid = item.Value;
            int CurrentRow = SAPBoAddon.B1Assistant.Matrix.AutoAddNewLine(this.CurrentForm, "mtx_2", "col_f", "@AVA_FF_FEE2", -1, "Lineid", "Lineid", true, "col_15");
            if (CurrentRow == -1) return;
            item = oMtx2.GetCellSpecific("col_lineid", CurrentRow);
            item.Value = Lineid;
        }
        /// <summary>
        /// 费用项删除行
        /// </summary>
        private void DeleteRow()
        {
            SAPbouiCOM.Matrix oMtx1 = CurrentForm.Items.Item("mtx_1").Specific;
            SAPbouiCOM.Matrix oMtx2 = CurrentForm.Items.Item("mtx_2").Specific;
            SAPbouiCOM.DBDataSource dbds = CurrentForm.DataSources.DBDataSources.Item("@AVA_FF_FEE2");

            int SelectedRow = oMtx1.GetNextSelectedRow();

            string LineId = string.Empty;
            dynamic Item = oMtx1.GetCellSpecific("col_f", SelectedRow);
            LineId = Item.Value;

            for (int i = 0; i < dbds.Size; i++)
            {
                if (dbds.GetValue("U_Lineid", i).Trim() == LineId)
                {
                    dbds.Offset = i;
                    int row = SAPBoAddon.B1Assistant.Matrix.GetMatrixRow(oMtx2, dbds, "col_f");
                    dbds.RemoveRecord(i);
                    if (row > 0 && row <= oMtx2.VisualRowCount)
                    {
                        oMtx2.DeleteRow(row);
                    }
                    i--;
                }
            }
            SAPBoAddon.B1Assistant.Matrix.DeleteRowAndDbdsLine(CurrentForm, "@AVA_FF_FEE1", "Lineid", "mtx_1", "col_f", SelectedRow, true);
        }
        /// <summary>
        /// 选择费用项
        /// </summary>
        /// <param name="SelectedRow"></param>
        private void SelectedRow(string ColUID, int Row)
        {
            if (CurrentForm.Mode == BoFormMode.fm_FIND_MODE) return;
            SAPbouiCOM.Matrix oMtx1 = CurrentForm.Items.Item("mtx_1").Specific;
            SAPbouiCOM.Matrix oMtx2 = CurrentForm.Items.Item("mtx_2").Specific;
            SAPbouiCOM.DBDataSource dbds = CurrentForm.DataSources.DBDataSources.Item("@AVA_FF_FEE2");
            //CurrentForm.Freeze(true);
            int SelectedRow = -1;
            if (Row > 0 && ColUID != "col_f")
            {
                SAPbouiCOM.CellPosition oPosition = oMtx1.GetCellFocus();
                if (oPosition == null) return;
                SelectedRow = oPosition.rowIndex;
                oMtx1.SelectRow(SelectedRow, true, false);
            }
            else
            {
                if (Row > 0 && oMtx1.GetCellFocus() != null)
                {
                    SelectedRow = oMtx1.GetNextSelectedRow();
                    if (CurrentForm.ActiveItem == "mtx_1")
                    {
                        SAPbouiCOM.CellPosition oPosition = oMtx1.GetCellFocus();
                        if (oPosition == null) return;
                        oMtx1.SetCellFocus(SelectedRow, oPosition.ColumnIndex);
                    }
                }
                else
                {
                    SelectedRow = Row;
                }
            }
            if (SelectedRow > 0)
            {
                string LineId = string.Empty;
                dynamic Item = oMtx1.GetCellSpecific("col_f", SelectedRow);
                LineId = Item.Value;
                if (CurrentForm.Mode != BoFormMode.fm_OK_MODE && CurrentForm.Mode != BoFormMode.fm_VIEW_MODE)
                    SAPBoAddon.B1Assistant.Matrix.UpdateDataTodbDataSource(CurrentForm, oMtx2, dbds, "col_f", "LineId", "col_15");
                oMtx2.Clear();
                for (int i = 0; i < dbds.Size; i++)
                {
                    if (dbds.GetValue("U_Lineid", i).Trim() == LineId)
                    {
                        dbds.Offset = i;
                        oMtx2.AddRow();
                    }
                }
                if (CurrentForm.Mode == BoFormMode.fm_ADD_MODE || CurrentForm.Mode == BoFormMode.fm_UPDATE_MODE)
                    AddRow();
            }
            else
            {
                if (CurrentForm.Mode != BoFormMode.fm_OK_MODE && CurrentForm.Mode != BoFormMode.fm_VIEW_MODE)
                    SAPBoAddon.B1Assistant.Matrix.UpdateDataTodbDataSource(CurrentForm, oMtx2, dbds, "col_f", "LineId", "col_15");
                oMtx2.Clear();
                for (int i = 0; i < dbds.Size; i++)
                {
                    dbds.Offset = i;
                    oMtx2.AddRow();
                }
            }
            //CurrentForm.Freeze(false);
        }
        #endregion
    }
}
